package com.seeyon.apps.enxicun.event;

import com.aspose.imaging.internal.bI.S;
import com.seeyon.apps.enxicun.utils.HttpRequest;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.event.AddDepartmentEvent;
import com.seeyon.ctp.organization.event.DeleteDepartmentEvent;
import com.seeyon.ctp.organization.event.UpdateDepartmentEvent;
import com.seeyon.ctp.organization.event.UpdateMemberEvent;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.ctp.util.json.JSONUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname DepartmentEventListener
 * @Description 部门监听
 * @Date 2022/3/24 15:52
 * @Created by Administrator
 */
public class DepartmentEventListener {
    private static Log log = LogFactory.getLog(MemberEventListener.class);// 打印日志
    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static String ip = AppContext.getSystemProperty("enxicun.ip");
    private static String access_token = AppContext.getSystemProperty("enxicun.access_token");
    private static V3xOrgDepartment department = null;
    Map<String, Object> hashMap = null;

    @ListenEvent(
            event = UpdateDepartmentEvent.class,
            async = true
    )
    public void updateDepartment(UpdateDepartmentEvent event) {
        log.info("修改部门监听");
        department = event.getDept();
        log.info("department转json:" + JSONUtil.toJSONString(department));
        //获取该部门上一级的单位信息
        Long orgAccountId = department.getOrgAccountId();
        V3xOrgAccount account = null;
        try {
            account = orgManager.getAccountById(orgAccountId);
        } catch (BusinessException e) {
            log.error(e);
        }
        String code = "DW_" + account.getCode();


        hashMap = new HashMap<String, Object>();
        hashMap.put("name", department.getName());
        hashMap.put("code", department.getCode());
        hashMap.put("parentCode", code);
        hashMap.put("sortNo", department.getSortId());
        log.info(JSONUtil.toJSONString(hashMap));
        try {
            HttpRequest.getJsonData(ip + "/api/department/add?access_token=" + access_token, JSONUtil.toJSONString(hashMap));
        } catch (Exception e) {
            log.error(e);
        }

    }

    @ListenEvent(
            event = DeleteDepartmentEvent.class,
            async = true
    )
    public void deleteDepartment(DeleteDepartmentEvent event) {
        log.info("删除部门监听");

        department = event.getDept();
        String code = department.getCode();

        try {
            HttpRequest.getJsonData(ip + "/api/department/delete/" + code + "?access_token=" + access_token, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ListenEvent(
            event = AddDepartmentEvent.class,
            async = true
    )
    public void addDepartment(AddDepartmentEvent event) throws Exception {
        log.info("新增部门监听");
        department = event.getDept();
        log.info("department转json:" + JSONUtil.toJSONString(department));
        //获取该部门上一级的单位信息
        Long orgAccountId = department.getOrgAccountId();
        V3xOrgAccount account = orgManager.getAccountById(orgAccountId);
        String code = "DW_" + account.getCode();


        hashMap = new HashMap<String, Object>();
        hashMap.put("name", department.getName());
        hashMap.put("code", department.getCode());
        hashMap.put("parentCode", code);
        hashMap.put("sortNo", department.getSortId());
        log.info(JSONUtil.toJSONString(hashMap));
        HttpRequest.getJsonData(ip + "/api/department/add?access_token=" + access_token, JSONUtil.toJSONString(hashMap));

    }
}
